<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		
		<script type="text/javascript">
			//1. then的链式简单实现；
			
			// p.then().then().then()
			
			class MyPromise{
				constructor(handle) {
				    this['status'] = "pedding";
					this['PromiseResult'] = undefined;
					
					handle(this._resolve.bind(this),this._reject.bind(this));
				}
				_resolve(arg){
					// console.log(this);
					this['status'] = "fufilled";
					this['PromiseResult'] = arg;
				}
				_reject(arg){
					this['status'] = "rejected";
					this['PromiseResult'] = arg;
				}
				then(onResolved,onRejected){
					console.log("then");
					onResolved && onResolved(this.PromiseResult);
					// then的链式操作 实现 1.返还this 2.返还实例化对象；
					return this;
					// return new MyPromise(res=>{res()});
				}
			}
			
			
			let p = new MyPromise((res,rej)=>{
				// 外部执行
				// res---->this._reslove  
				res("success");
				// rej("error");
				// rej--->this._reject
			});
			// console.log(p);
			p.then((res)=>{
				console.log(res);
			},(err)=>{
				
			}).then().then().then();
			
			
		</script>
	</body>
</html>
